#include"process.h"
#include<string.h>
#include<unistd.h>

#define LENGTH 101
#define LABLE '#'

//version2
void FlushProcess(double target, double current)
{
    // step 0: 定义光标
    const static char *sym = "|/-\\";
    static int index = 0;
    int len = strlen(sym);

    // step 1: 更新出进度
    double rate = (current / target)*100.0; // 5.0 100.0 -> 0.05 -> 5.0
    // step 2: 整数个进度递增，刷新一个#
    int cnt = (int)rate;
    // step3: 构建缓冲区
    char bar[LENGTH];
    memset(bar, 0, sizeof(bar));
    for(int i = 0; i < cnt; i++)
        bar[i] = LABLE;
    // step4: 刷新
    printf("[%-100s][%.1lf%%][%c]\r", bar, rate, sym[index++]);
    index %= len;
    fflush(stdout);

    // step 5: 处理更到到100%
    if(rate >= 100.0)
        printf("\n");
}

//示例教学，不能实际应用
//version1
void Process()
{
	const char* symbol = "|/-\\";
	int s_len = strlen(symbol);
	char bar[LENGTH];
	memset(bar,'\0',sizeof(bar));
	int cnt = 0;
	while(cnt <= 100)
	{
		printf("[%-100s][%d%%][%c]\r",bar,cnt,symbol[cnt%s_len]);
		fflush(stdout);
		bar[cnt++] = LABLE;
		usleep(50000);

	}
	printf("\n");

}
